<template>
  <el-dialog
    :title="!dataForm.fdId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="fdUserId">
      <el-input v-model="dataForm.fdUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="小说id" prop="fdBookId">
      <el-input v-model="dataForm.fdBookId" placeholder="小说id"></el-input>
    </el-form-item>
    <el-form-item label="目录id" prop="fdCatalogueId">
      <el-input v-model="dataForm.fdCatalogueId" placeholder="目录id"></el-input>
    </el-form-item>
    <el-form-item label="小说内容" prop="fdBookContentId">
      <el-input v-model="dataForm.fdBookContentId" placeholder="小说内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="fdCreationTime">
      <el-input v-model="dataForm.fdCreationTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="fdUpdateTime">
      <el-input v-model="dataForm.fdUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="评论状态" prop="fdStatus">
      <el-input v-model="dataForm.fdStatus" placeholder="评论状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          fdId: 0,
          fdUserId: '',
          fdBookId: '',
          fdCatalogueId: '',
          fdBookContentId: '',
          fdCreationTime: '',
          fdUpdateTime: '',
          fdStatus: ''
        },
        dataRule: {
          fdUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          fdBookId: [
            { required: true, message: '小说id不能为空', trigger: 'blur' }
          ],
          fdCatalogueId: [
            { required: true, message: '目录id不能为空', trigger: 'blur' }
          ],
          fdBookContentId: [
            { required: true, message: '小说内容不能为空', trigger: 'blur' }
          ],
          fdCreationTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          fdUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          fdStatus: [
            { required: true, message: '评论状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.fdId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fdId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookfragmentdiscuss/info/${this.dataForm.fdId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.fdUserId = data.tbBookFragmentDiscuss.fdUserId
                this.dataForm.fdBookId = data.tbBookFragmentDiscuss.fdBookId
                this.dataForm.fdCatalogueId = data.tbBookFragmentDiscuss.fdCatalogueId
                this.dataForm.fdBookContentId = data.tbBookFragmentDiscuss.fdBookContentId
                this.dataForm.fdCreationTime = data.tbBookFragmentDiscuss.fdCreationTime
                this.dataForm.fdUpdateTime = data.tbBookFragmentDiscuss.fdUpdateTime
                this.dataForm.fdStatus = data.tbBookFragmentDiscuss.fdStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbookfragmentdiscuss/${!this.dataForm.fdId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fdId': this.dataForm.fdId || undefined,
                'fdUserId': this.dataForm.fdUserId,
                'fdBookId': this.dataForm.fdBookId,
                'fdCatalogueId': this.dataForm.fdCatalogueId,
                'fdBookContentId': this.dataForm.fdBookContentId,
                'fdCreationTime': this.dataForm.fdCreationTime,
                'fdUpdateTime': this.dataForm.fdUpdateTime,
                'fdStatus': this.dataForm.fdStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
